<!--
 * @Author: wangming
 * @Date: 2020-09-15 16:47:30
 * @LastEditors: wangming
 * @LastEditTime: 2022-04-20 16:13:05
 * @Description: file content
-->
<template>
  <el-radio-group
    v-if="!viewInfo"
    @input="inputbox"
    :value="value"
    :disabled="disabled"
  >
    <el-radio :label="i.value" v-for="(i, index) in radiolist" :key="index">
      {{ i.label }}
    </el-radio>
  </el-radio-group>
  <div
    v-else
    :style="{
      display: 'inline-block',
      whiteSpace: 'break-spaces',
      width: config.__config.width + '%',
    }"
  >
    {{ viewLable }}
  </div>
</template>

<script>
import myMixin from "@/pageComponents/myMixin";
import vmMixin from "@/pageComponents/vmMixin";
import { postHmhttprequestdefinitionExecute } from "@/apis/paas/HmHttpRequestDefinitionController.js";
import { dataToList } from "@/utils/util";
import { getHmdictionaryOptions } from "@/apis/paas/HmDictionaryController.js";
import { getVueComponent } from "@/utils/common.js";
export default {
  mixins: [myMixin, vmMixin],
  data() {
    return {
      radiolist: [],
    };
  },
  vmEvents: {
    getSelectData() {
      return this.radiolist.filter((i) => {
        return i.value == this.value;
      });
    },
  },
  async created() {
    await this.getApiOptions();
    this.changeChildForm(this.value);
  },
  watch: {
    value(v) {
      this.changeChildForm(v);
    },
  },
  computed: {
    viewLable() {
      const { label } =
        this.radiolist.find((item) => item.value === this.value) || {};
      return label;
    },
    dataLoadFunction() {
      const { dataCustomLoadFn } = this.config.__config;
      return getVueComponent(dataCustomLoadFn);
    },
  },
  methods: {
    getId(id) {
      return this.config.__config.codeType === "RELATION_REFERENCE"
        ? id
        : `[${id}]`;
    },
    changeChildForm(id) {
      if (!!this.descForm) {
        const { label } =
          this.radiolist.find((item) => item.value === id) || {};
        this.descForm[this.config.modelName] = label;
      }
    },
    inputbox(v) {
      this.$emit("input", v);
      this.runEventName("change", { values: v });
    },
    async getApiOptions() {
      const {
        modelKey,
        relationType,
        valueTeam,
        modelCode,
        dataCustomLoad,
        dataCustomLoadFn,
      } = this.config.__config;
      if (dataCustomLoad && dataCustomLoadFn) {
        let options = await this.dataLoadFunction(
          this.vmEvents?.context,
          this.vmEvents?.exports,
          {
            type: relationType,
            value: relationType === "DICTIONARY" ? valueTeam : modelCode,
          }
        );
        this.radiolist = dataToList(options).map((item) => {
          return {
            value: this.getId(item.id),
            ...item,
          };
        });
      } else if (relationType === "DICTIONARY") {
        let data = await getHmdictionaryOptions(
          { type: this.config.__config.valueTeam },
          { useCache: true }
        );
        this.radiolist = data.map(({ optionValue, optionName, localism }) => ({
          label: this.$at(localism) || optionName,
          value: this.getId(optionValue),
        }));
      } else if (!!modelKey) {
        const data = await postHmhttprequestdefinitionExecute(
          {
            key: "selectList",
            modelKey,
            values: {
              fieldCode: this.config.modelName,
              _RELATION_TYPE_: "BUSINESS",
            },
          },
          { useCache: true }
        );
        this.radiolist = data.map((item) => {
          return {
            value: this.getId(item.id),
            ...item,
          };
        });
      }
    },
  },
};
</script>
